package com.taotao.netty.socket.multiclient.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;


/**
 * @Description: 多个客户端通信的服务器端
 * @Author: TaoJF
 * @Date: Created in 2018/6/28 下午9:24
 */
public class MyChatServer {

    public static void main(String[] args) {
        System.out.println("MyChatServer 开始启动...");
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new MyChatServerInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
            channelFuture.channel().closeFuture().sync();

            System.out.println("可发布公告...");
//            ChannelGroup channelGroup = MyChatServerHandler.getClientChannelGroup();
//            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//            channelGroup.forEach(channel -> {
//                try {
//                    channel.writeAndFlush(String.format("服务器通知：%s\r\n", br.readLine()));
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }


    }

}
